/**
 * @Description:
 * @author SunYanMing<95317356@qq.com>
 * @date 2024/12/6
 */

import {
  intersectionWith,
  isEqual,
  mergeWith,
  isFunction,
  unionWith,
  isArray,
  isObject,
} from 'lodash-es';
import { unref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { ElMessageBoxOptions } from 'element-plus';

type MessageType = '' | 'success' | 'warning' | 'info' | 'error';
/**
 * Recursively merge two objects.
 * 递归合并两个对象。
 *
 * @param source The source object to merge from. 要合并的源对象。
 * @param target The target object to merge into. 目标对象，合并后结果存放于此。
 * @param mergeArrays How to merge arrays. Default is "replace".
 *        如何合并数组。默认为replace。
 *        - "union": Union the arrays. 对数组执行并集操作。
 *        - "intersection": Intersect the arrays. 对数组执行交集操作。
 *        - "concat": Concatenate the arrays. 连接数组。
 *        - "replace": Replace the source array with the target array. 用目标数组替换源数组。
 * @returns The merged object. 合并后的对象。
 */
export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>(
  source: T,
  target: U,
  mergeArrays: 'union' | 'intersection' | 'concat' | 'replace' = 'replace',
): T & U {
  if (!target) {
    return source as T & U;
  }
  if (!source) {
    return target as T & U;
  }
  return mergeWith({}, source, target, (sourceValue, targetValue) => {
    if (isArray(targetValue) && isArray(sourceValue)) {
      switch (mergeArrays) {
        case 'union':
          return unionWith(sourceValue, targetValue, isEqual);
        case 'intersection':
          return intersectionWith(sourceValue, targetValue, isEqual);
        case 'concat':
          return sourceValue.concat(targetValue);
        case 'replace':
          return targetValue;
        default:
          throw new Error(`Unknown merge array strategy: ${mergeArrays as string}`);
      }
    }
    if (isObject(targetValue) && isObject(sourceValue)) {
      return deepMerge(sourceValue, targetValue, mergeArrays);
    }
    return undefined;
  });
}

// dynamic use hook props
declare type Recordable<T = any> = Record<string, T>;
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
  const ret: Recordable = {};

  Object.keys(props).map((key) => {
    ret[key] = unref((props as Recordable)[key]);
  });

  return ret as Partial<U>;
}

export const projectName = 'BaseModelForm';

export function log(...message: any[]) {
  console.log(`[${projectName} log]:`, ...message);
}
export function warn(...message: string[]) {
  console.warn(`[${projectName} warn]:`, ...message);
}
export function error(message: string) {
  throw new Error(`[${projectName} error]:${message}`);
}

export async function confirm(content: string) {
  return await ElMessageBox.confirm(content, '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  });
}

// 提示
export function alert(content: string, options: ElMessageBoxOptions = { type: 'warning' }) {
  ElMessageBox.alert(content, '系统提示', options);
}

// 提示
export function message(content: string, type: MessageType = 'warning') {
  // @ts-ignore
  const msg = isFunction(ElMessage[type]) ? ElMessage[type] : ElMessage.warning;
  return msg({
    message: content,
  });
}
